package com.woniu.supermarket.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Goods;
import com.woniu.supermarket.entity.Purchase;
import com.woniu.supermarket.entity.PurchaseCheck;
import com.woniu.supermarket.entity.PurchaseDetail;
import com.woniu.supermarket.mapper.GoodsMapper;
import com.woniu.supermarket.mapper.PurchaseCheckMapper;
import com.woniu.supermarket.mapper.PurchaseDetailMapper;
import com.woniu.supermarket.mapper.PurchaseMapper;
import com.woniu.supermarket.service.IPurchaseService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.PurchaseDetailVo;
import com.woniu.supermarket.vo.PurchaseVo;
import com.woniu.supermarket.vo.SearchPurchaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PurchaseServiceImpl implements IPurchaseService {

    @Autowired
    PurchaseMapper purchaseMapper;

    @Autowired
    PurchaseDetailMapper purchaseDetailMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    PurchaseCheckMapper purchaseCheckMapper;

    @Override
    @Transactional
    public int addPurchase(Purchase purchase) {
        purchase.setPurchaseNumber(CodeCreateUtil.createPurchaseCode());
        int i = purchaseMapper.insert(purchase);
        Integer id = purchase.getId();
        return i;
    }



    @Override
    @Transactional
    public int removeBatchIds(int[] ids) {
        int i = purchaseMapper.deleteBatchPurchase(ids);
        purchaseDetailMapper.deleteBatchPurchaseDetail(ids);
        return i;
    }


    /**
     * 修改订单以及订单详情
     * @param purchaseVo 接受数据的对象
     * @return
     */
    @Transactional
    @Override
    public int modifyPurchase(PurchaseVo purchaseVo) {
        //1.先将该订单下的订单详情删除
        purchaseDetailMapper.deletePurchaseDetailByPurchaseId(purchaseVo.getId());
        //2.修改订单信息
        purchaseMapper.updateByPurchaseVo(purchaseVo);
        //3.插入订单详情
        List<PurchaseDetailVo> purchaseDetailVoList = purchaseVo.getPurchaseDetailList();
        for (PurchaseDetailVo purchaseDetailVo : purchaseDetailVoList) {
            purchaseDetailVo.setPurchaseId(purchaseVo.getId());
            purchaseDetailVo.setPurchaseDetailNumber(CodeCreateUtil.createPurchaseDetailCode());
        }
        int i = purchaseDetailMapper.insertBatchPurchaseDetail(purchaseDetailVoList);

        return i;
    }

    /**
     * 根据采购订单id删除订单以及订单详情
     * @param id 采购订单id
     * @return
     */
    @Override
    @Transactional
    public int removePurchase(Integer id) {
        int i = purchaseMapper.deleteByPrimaryKey(id);
        purchaseDetailMapper.deletePurchaseDetailByPurchaseId(id);
        return i;
    }

    /**
     * 分页查询订单信息
     * @param now 当前页
     * @param size 当前页大小
     * @param searchPurchaseVo 封装搜索条件的对象
     * @return
     */
    @Override
    public PageInfo<PurchaseVo> findByCons(Integer now, Integer size, SearchPurchaseVo searchPurchaseVo) {
        PageHelper.startPage(now,size);
        List<PurchaseVo> lists = purchaseMapper.selectByCons(searchPurchaseVo);
        return new PageInfo<>(lists);
    }


    /**
     * 增加订单以及增加订单详情
     * @param purchaseVo
     * @return
     */
    @Transactional
    @Override
    public int addPurchaseOrder(PurchaseVo purchaseVo) {
        //先插入订单
        purchaseMapper.inertPurchaseOrder(purchaseVo);

        //插入订单详情
        List<PurchaseDetailVo> purchaseDetailVoList = purchaseVo.getPurchaseDetailList();
        for (PurchaseDetailVo purchaseDetailVo : purchaseDetailVoList) {
            purchaseDetailVo.setPurchaseId(purchaseVo.getId());
            purchaseDetailVo.setPurchaseDetailNumber(CodeCreateUtil.createPurchaseDetailCode());
        }
        int i = purchaseDetailMapper.insertBatchPurchaseDetail(purchaseDetailVoList);

        return i;
    }

    /**
     * 查询所有的订单
     * @return
     */
    @Override
    public List<PurchaseVo> findAll() {
        return purchaseMapper.selectAllPurchase();
    }

    /**
     * 根据采购订单号查询
     * @param purchaseNumber 采购订单号
     * @return
     */
    @Override
    public Purchase findPurchaseByPurchaseNumber(String purchaseNumber) {
        return purchaseMapper.selectPurchaseByPurchaseNumber(purchaseNumber);
    }

    /**
     * 根据采购订单号查询订单和订单详情
     * @param purchaseNumber 采购订单号
     * @return
     */
    @Override
    public PurchaseVo findPurchaseAndDetailByPurchaseNumber(String purchaseNumber) {
        return purchaseMapper.selectPurchaseAndDetailByPurchaseNumber(purchaseNumber);
    }


    /**
     * 审核订单方法
     * @param purchaseCheck 用来接收审核信息的对象
     * @return
     */
    @Transactional
    @Override
    public int auditPurchaseCheck(PurchaseCheck purchaseCheck) {
        //1.先修改采购订单状态
        int status = purchaseCheck.getCheckResult();
        String purchaseNumber = purchaseCheck.getPurchaseNumber();
        purchaseMapper.updateStatusByPurchaseNumber(purchaseNumber,status);

        //2.将审核信息插入到审核信息表
        int i = purchaseCheckMapper.insert(purchaseCheck);
        return i;
    }

    @Override
    public PageInfo<PurchaseVo> findByCons2(Integer now, Integer size) {
        PageHelper.startPage(now,size);
        List<PurchaseVo> lists = purchaseMapper.selectAllPurchase();
        return new PageInfo<>(lists);
    }
}
